package com.leyou.item.controller;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author CHERRY
 * @date 2018-09-01-16:02
 */
@Controller
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 根据Pid 查询分类
     *
     * @param pid
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryCategoryListByParentId(@RequestParam(value = "pid", defaultValue = "0") Long pid) {

        try {
            if (pid == null || pid.longValue() < 0) {
                //pid 为空或小于0 参数错误 响应400
                return ResponseEntity.badRequest().build();
            }
            //查询
            List<Category> categories = categoryService.queryCategoryListByParentId(pid);
            if (CollectionUtils.isEmpty(categories)) {
                //结果集为空 响应404
                return ResponseEntity.notFound().build();
            }
            // 200
            return ResponseEntity.ok(categories);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //异常 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryBrandCategoryByBid(@PathVariable("bid") Long bid) {
        List<Category> categories = this.categoryService.queryBrandCategoryByBid(bid);
        if (CollectionUtils.isEmpty(categories)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(categories);
    }

    /**
     * 根据分类id集合查询分类名称
     *
     * @param ids
     * @return
     */
    @GetMapping("names")
    public ResponseEntity<List<String>> queryCategoryNamesByIds(@RequestParam("ids") List<Long> ids) {
        List<String> names = this.categoryService.queryCategoryNamesByIds(ids);
        if (CollectionUtils.isEmpty(names)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(names);
    }

    /**
     * 根据三级id查询 category 和 parent category
     *
     * @param cid
     * @return
     */
    @GetMapping("all")
    public ResponseEntity<List<Category>> queryAllCategoryByCid3(@RequestParam("cid") Long cid) {
        List<Category> categories = this.categoryService.queryCategoryByCid(cid);
        if (CollectionUtils.isEmpty(categories)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(categories);

    }

}
